import Request from 'luch-request'

const http = new Request({
	baseURL: 'http://47.108.225.82:8080'
})

http.interceptors.request.use(config => {
	const token = uni.getStorageSync('token')
	if (token) {
		config.header.Authorization = `Bearer ${token}`
	}
	return config
}, config => { // 可使用async await 做异步操作
	return Promise.reject(config)
})

http.interceptors.response.use(response => {
	const responseCode = response.data.code
	if (responseCode === 401) {
		uni.clearStorageSync()
		uni.showToast({
			title: '登录失效，请重新登录',
			position: 'bottom',
			icon: 'error'
		})
		setTimeout(() => {
			uni.navigateTo({
				url: '/pages/login/index'
			})
		}, 1000)
	} else {
		Object.assign(response.data, {
			status: response.data.code === 200
		})
	}
	return response.data
}, response => { /*  对响应错误做点什么 （statusCode !== 200）*/
	return Promise.reject(response)
})

interface PageParams {
    /** @description 页码 */
    pageNum: number
    /** @description 条数 */
    pageSize: number
}

export {
	http,
	PageParams
}
